package com.caiyouxi.boss.buser.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.boss.buser.mapper.SysRightsMapper;
import com.caiyouxi.boss.buser.mapper.SysRolesMapper;
import com.caiyouxi.boss.buser.mapper.SysUserProfileMapper;
import com.caiyouxi.boss.buser.model.SysRightsBean;
import com.caiyouxi.boss.buser.model.SysRolesBean;
import com.caiyouxi.boss.buser.model.SysUserProfileBean;
import com.caiyouxi.boss.buser.service.SysRightsService;
import com.caiyouxi.boss.buser.service.SysRolesService;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.ListDistinctUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/26.
 */
@Service
public class SysRightsServiceImpl extends BaseService implements SysRightsService{

    @Autowired
    private SysRightsMapper sysRightsMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysRolesMapper sysRolesMapper;
    @Autowired
    private SysRolesService sysRolesService;
    @Autowired
    private SysUserProfileMapper userProfileMapper;


    //获取权限列表
    @Override
    public ResData get_right_list(SysRightsBean bean) {

        //获取所有的权限
        List<Integer> right_id = sysRightsMapper.get_rights_id();
        //获取一级权限列表
        List<SysRightsBean> one_list = sysRightsMapper.get_rights_list(right_id);
        JSONArray one_arr = new JSONArray();
        for(SysRightsBean srb:one_list){
            //转换成json对象
            JSONObject obj = (JSONObject)JSONObject.toJSON(srb);
            //获取二级权限列表
            List<SysRightsBean> two_list = sysRightsMapper.get_rights_evel(srb.getId(),right_id);
            JSONArray two_arr = new JSONArray();
            for(SysRightsBean two_srb:two_list){
                JSONObject two_obj = (JSONObject)JSONObject.toJSON(two_srb);
                //获取三级目录
                List<SysRightsBean> three_list = sysRightsMapper.get_rights_evel(two_srb.getId(),right_id);
                JSONArray three_arr = (JSONArray) JSONArray.toJSON(three_list);
                for(SysRightsBean three_srb:three_list){
                    JSONObject three_obj = (JSONObject)JSONObject.toJSON(three_srb);
                    getObj(three_obj,three_srb);
                    three_arr.add(three_obj);
                }
                two_obj.put("list",three_arr);
                getObj(two_obj,two_srb);
                two_arr.add(two_obj);
            }
            obj.put("list",two_arr);
            getObj(obj,srb);
            one_arr.add(obj);
        }
        JSONObject right_json = new JSONObject();
        right_json.put("all-rights",one_arr);
        return success(right_json);

    }

    public void getObj(JSONObject obj,SysRightsBean bean){

        //取出URL
        JSONObject json = JSONObject.parseObject(bean.getMenu());
        String url = json.get("url").toString();
        bean.setUrl(url);
        obj.put("url",url);
        int role_count = sysRolesMapper.get_rights_role_count(bean);
        bean.setRole_count(role_count);
        obj.put("role_count",role_count);
        int user_id = 12;
        bean.setUser_count(user_id);
        obj.put("user_id",user_id);
    }

    //添加权限信息
    @Override
    public ResData add_rights(SysRightsBean bean) {

        int result = sysRightsMapper.add_sys_rights(bean);
        if(result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
    }

    //获取权限信息
    @Override
    public ResData get_right_info(SysRightsBean bean) {
        SysRightsBean  srb_bean = sysRightsMapper.select_sys_rights(bean);
        return success(srb_bean);
    }

    //修改权限信息
    @Override
    public ResData update_rights(SysRightsBean bean) {
        int result = sysRightsMapper.update_sys_rights(bean);
        if(result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
    }

    //删除权限信息
    @Override
    public ResData delete_rights(SysRightsBean bean) {

        int result = sysRightsMapper.delete_sys_rights(bean);
        if(result == 1){
           return success(null);
        }
        return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
    }

    //通过权限id查询角色信息
    @Override
    public ResData get_right_role_list(SysRightsBean bean) {
        List<SysRolesBean> roles_list = sysRightsMapper.get_right_role_list(bean);
        return success(roles_list);
    }

    //更新角色权限信息
    @Override
    public ResData update_role_right(SysRightsBean bean) {
        SysRolesBean roles_bean = new SysRolesBean();
        roles_bean.setId(bean.getRole_id());
        SysRolesBean roles_rights = sysRolesMapper.get_role_by_id(roles_bean);
        String rights = roles_rights.getRights();
        if(rights.indexOf(bean.getId().toString())>0){
            return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
        }else{
            String new_rights = rights+bean.getId().toString()+"|";
            roles_bean.setRights(new_rights);
            int result = sysRightsMapper.update_role_right(roles_bean);
            if(result == 1){
              return success(null);
            }
        }
        return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
    }

    //通过权限id查询拥有此权限的用户信息
    @Override
    public ResData get_right_user_list(SysRightsBean bean) {

        List<SysRolesBean> role_list = sysRightsMapper.get_right_role_list(bean);
        List<SysUserProfileBean> user_list = new ArrayList<>();
         for(SysRolesBean role_bean:role_list){
             List<SysUserProfileBean> list = sysRolesMapper.get_user_role_id(role_bean);
             for(SysUserProfileBean user_bean:list){
                 user_list.add(user_bean);
             }
        }
        //list去重
        List<SysUserProfileBean> dis_list = new ArrayList<>();
        user_list.stream().forEach(
                p -> {
                    if (!dis_list.contains(p)) {
                        dis_list.add(p);
                    }
                }
        );
        return success(dis_list);
    }

    //添加用户权限信息
    @Override
    public ResData update_user_right_info(SysRightsBean right_bean) {
        SysUserProfileBean bean = new SysUserProfileBean();
        bean.setId(right_bean.getUser_id());
        SysUserProfileBean user_bean = userProfileMapper.get_sys_role_list(bean);
        String role = user_bean.getRoles();
        String[] roles = ListDistinctUtils.get_rights_string(role);
        for(String role_id:roles) {
            right_bean.setRole_id(Integer.valueOf(role_id));
            SysRolesBean role_bean = new SysRolesBean();
            role_bean.setId(right_bean.getRole_id());
            SysRolesBean roles_rights = sysRolesMapper.get_role_by_id(role_bean);
            String rights = roles_rights.getRights();
            if(rights.indexOf(right_bean.getId().toString())>0){
                return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
            }else{
                String new_rights = rights+right_bean.getId()+"|";
                role_bean.setRights(new_rights);
                int result = sysRightsMapper.update_role_right(role_bean);
                if(result>0){
                    return success(null);
                }
            }
        }
        return failed(ErrorCode.SYS_RIGHTS_ERROR.getCode(),ErrorCode.SYS_RIGHTS_ERROR.getMsg(),null);
    }
}
